package com.aya.tb.context;

import io.netty.channel.ChannelHandlerContext;
import org.springframework.stereotype.Service;

import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.List;

@Service
public class StandardParameterResolver implements ParameterResolver {
    List<ArgumentSupportHandler> argumentSupportHandlerList = new ArrayList<>();

    @Override
    public Object[] resolve(Parameter[] parameters, byte[] body, ChannelHandlerContext ctx) {
        Object[] objects = new Object[parameters.length];
        for (int i = 0; i < parameters.length; i++) {
            Parameter parameter = parameters[i];
            for (ArgumentSupportHandler argumentSupportHandler : argumentSupportHandlerList) {
                if (argumentSupportHandler.support(parameter, body, ctx)) {
                    objects[i] = argumentSupportHandler.handler(parameter, body, ctx);
                }
            }
        }


        return objects;
    }

    public void addArgumentHandler(ArgumentSupportHandler value) {
        argumentSupportHandlerList.add(value);
    }
}
